package com.bbc.service.product.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bbc.base.BaseMessage;
import com.bbc.base.page.MysqlPaginatedArrayList;
import com.bbc.base.page.PaginatedList;
import com.bbc.entity.product.Product;
import com.bbc.entity.product.ProductQuery;
import com.bbc.entity.product.ProductShowTypeEnum;
import com.bbc.entity.user.User;
import com.bbc.mapper.product.ProductMapper;
import com.bbc.service.product.ProductService;
import com.bbc.third.taobao.TBApi;
import com.opensymphony.oscache.util.StringUtil;
import com.taobao.api.response.ItemGetResponse;

@Service
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private transient TBApi tBApi;
	
	@Autowired
	private transient ProductMapper productMapper;

	@Override
	public Integer addProduct(Product product) {
		return productMapper.addProduct(product);
	}

	@Override
	public List<Product> findProduct(ProductQuery query, int page, int pageSize) {
		PaginatedList<Product> list = new MysqlPaginatedArrayList<Product>(page,pageSize);
		list.setTotalItem(productMapper.findProductCount(query));
		query.setStartRow(list.getStartRow());
		query.setEndRow(list.getEndRow());
		list.addAll(productMapper.findProduct(query));
		return list;
	}

	@Override
	public Product findProductById(Integer id) {
		if (id == null) {
			return null;
		}
		ProductQuery q = new ProductQuery();
		q.setId(id);
		int count = productMapper.findProductCount(q);
		if (count == 1) {
			return productMapper.findProduct(q).get(0);
		} else {
			return null;
		}
	}

	@Override
	public Product findProductByNumIid(Long numIid, String fromKey) {
		if (numIid == null || StringUtil.isEmpty(fromKey)) {
			return null;
		}
		ProductQuery q = new ProductQuery();
		q.setNumIid(numIid);
		q.setFromKey(fromKey);
		int count = productMapper.findProductCount(q);
		if (count == 1) {
			return productMapper.findProduct(q).get(0);
		} else {
			return null;
		}
	}

	@Override
	public void updateProduct(Product product) {
		productMapper.updateProduct(product);
	}

	@Override
	public BaseMessage addProductFromTBApi(Long numIid, User u, Integer showType) {
		BaseMessage msg = new BaseMessage();
		if (numIid == null) {
			msg.setCode(101);
			msg.setInfo("numIid为空");
		} else {
			ProductQuery q = new ProductQuery();
			q.setNumIid(numIid);
			q.setFromKey(tBApi.key);
			int count = productMapper.findProductCount(q);
			if (count == 1) {
				msg.setCode(109);
				msg.setInfo("数据已存在");
			} else {
				ItemGetResponse response = tBApi.getProduct(numIid);
				System.out.println(response.getBody());
				Product p = new Product();
				p.setApproveStatus(response.getItem().getApproveStatus());
				p.setCid(response.getItem().getCid().toString());
				p.setDetailUrl(response.getItem().getDetailUrl());
				p.setFromKey(tBApi.key);
				if (showType == null) {
					showType = ProductShowTypeEnum.SHOW_USER_SHARE.getKey();
				}
				p.setShowType(showType);
				p.setNick(response.getItem().getNick());
				p.setNumIid(numIid);
				p.setPicUrl(response.getItem().getPicUrl());
				p.setPrice(response.getItem().getPrice());
				p.setTitle(response.getItem().getTitle());
				p.setType(response.getItem().getType());
				if (u!=null) {
					p.setUserId(u.getId());
					p.setUserNick(u.getNick());
				}
				productMapper.addProduct(p);
				msg.setCode(100);
				msg.setInfo("操作成功");
				msg.setSuccess(true);
			}
		}
		return msg;
	}

	@Override
	public Integer findProductCount(ProductQuery query) {
		return productMapper.findProductCount(query);
	}

}
